<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 板式接口
 */
class Tpl extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    /**
     * 权限Auth
     * @var \app\common\model\Tpl
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Tpl();
    }

    /**
     * 所有数据
     * @ApiParams   (name="name", type="string", required=true, description="名称")
     */
    public function listAll()
    {
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        $name = $this->request->request("name");
        if(empty($name)){
            $this->success('success', $this->model->select());
        }else{
            $this->success('success', $this->model->where('name','like',$name)->select());
        }

    }

    /**
     * 新增
     *
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="name", type="string", required=true, description="名称")
     * @ApiParams   (name="subtitle", type="string", required=true, description="副标题")
     * @ApiParams   (name="page", type="string", required=true, description="页面")
     * @ApiParams   (name="content", type="string", required=true, description="内容，json格式字符串")
     *
     */
    public function add()
    {
//        $name = $this->request->post('name');
//        $page = $this->request->post('page');
//        $content = $this->request->post('content');
//        print_r($this->request->post(false));
//        print_r($_POST['content']);
//        print_r(json_decode(strip_tags($content),true));die;
//        if (!$name || !$page || !$content) {
//            $this->error(__('Invalid parameters'));
//        }
//        $ret = $this->model->save(['name'=>$name,'page'=>$page,'content'=>$content]);
        $param = $this->request->post(false);
        $param['status'] = 'normal';
        $ret = $this->model->save($param);
        if ($ret) {
            $this->success('success', $this->model->getLastInsID());
        } else {
            $this->error($this->model->getError());
        }
    }

    /**
     * 详情
     *
     * @ApiMethod   (GET)
     * @ApiParams   (name="id", type="int", required=true, description="主键id")
     *
     */
    public function detail()
    {
        $id = $this->request->param('id');
        $row = $this->model->get(['id' => $id]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->success('success', $row);
    }

    /**
     * 编辑
     *
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="name", type="string", required=true, description="名称")
     * @ApiParams   (name="subtitle", type="string", required=true, description="副标题")
     * @ApiParams   (name="page", type="string", required=true, description="页面")
     * @ApiParams   (name="content", type="string", required=true, description="内容，json格式字符串")
     * @ApiParams   (name="id", type="int", required=true, description="主键id")
     *
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $row = $this->model->get(['id' => $id]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $param = $this->request->post(false);
        $ret = $this->model->where(['id'=>$id])->update($param);
        if ($ret) {
            $this->success('success');
        } else {
            $this->error($this->model->getError());
        }
    }

    /**
     * 删除
     *
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="id")
     *
     */
    public function del()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->model->where('id', '=', $id)->delete();
        if ($ret) {
            $this->success('success');
        } else {
            $this->error($this->model->getError());
        }
    }
}
